package com.yhf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhf.dto.BranchDto;
import com.yhf.dto.TaskPlanDto;
import com.yhf.mapper.BranchMapper;
import com.yhf.mapper.PMapper;
import com.yhf.mapper.TaskPlanMapper;
import com.yhf.model.Branch;
import com.yhf.model.Stage;
import com.yhf.model.Taskplan;
import com.yhf.service.TaskPlanService;
import com.yhf.utils.PageResult;
import com.yhf.utils.QueryPageBean;
import com.yhf.utils.Result;
import com.yhf.vo.BranchEmg;
import com.yhf.vo.TaskPlanBranch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TaskPlanServiceImpl extends ServiceImpl<TaskPlanMapper, Taskplan> implements TaskPlanService {
    @Autowired
    TaskPlanMapper taskPlanMapper;
    @Autowired
    PMapper pMapper;
    @Autowired
    BranchMapper branchMapper;

    @Override
    public PageResult pageList(QueryPageBean queryPageBean) {
        Page<TaskPlanDto> page = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        QueryWrapper<TaskPlanDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        IPage<TaskPlanDto> page1 = taskPlanMapper.selectAll(page, queryWrapper);
        PageResult pageResult = new PageResult();
        pageResult.setRows(page1.getRecords());
        pageResult.setTotal(page1.getTotal());
        return pageResult;
    }

    @Override
    public Result findBranch(String bid) {
        TaskPlanBranch taskPlanBranch = new TaskPlanBranch();
        QueryWrapper<Branch> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        List<Branch> branches = branchMapper.selectList(queryWrapper);
        BranchEmg[] branchEmgs = new BranchEmg[branches.size()];
        for (int i = 0;i<branches.size();i++) {
            BranchEmg branchEmg = new BranchEmg();
            branchEmg.setLabel(branches.get(i).getBname());
            branchEmg.setValue(branches.get(i).getBid());
            branchEmgs[i]=branchEmg;
        }
        taskPlanBranch.setBranchEmgs(branchEmgs);
        if (bid != null) {
            taskPlanBranch.setChecked(bid);
        }
        Result result = new Result();
        result.setData(taskPlanBranch);
        result.setMessage("success");
        result.setFlag(true);
        return result;
    }

    @Override
    public Result Add(Taskplan taskplan) {
        int insert = taskPlanMapper.insert(taskplan);
        if (insert > 0) {
            return Result.success();
        }
        return Result.fail();

    }

    @Override
    public Result findTaskPlanById(String tid) {
        Result result = new Result();
        Taskplan taskplan = taskPlanMapper.selectById(tid);
        result.setData(taskplan);
        result.setMessage("success");
        result.setFlag(true);
        return result;
    }

    @Override
    public Result editByStage(Taskplan taskplan) {
        taskplan.setUpdatetime(new Date());
        int i = taskPlanMapper.updateById(taskplan);
        if (i > 0) {
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result deleteBySid(String tid) {
        Taskplan taskplan = new Taskplan();
        taskplan.setStatus(0);
        taskplan.setTid(tid);
        int delete = taskPlanMapper.updateById(taskplan);
        if (delete >0) {
            return Result.success();
        }
        return Result.fail();
    }
}
